// pages/first/search/search.js
const config = require('../../config');
const { API_BASE_URL } = config;
Page({
  data: {
    search: '',
    current: 1,
    size: 10,
    inputValue: '',  // 初始化输入值为空字符串
    isLoading: false,  // 添加加载状态
    /*historySearches: [],  // 存储历史搜索记录*/
    goodsList: [],
    highlightList: [],
    total: 0

  },
  onLoad(options) {

  },
  // 获取输入值
  getValue(event) {
    console.log(event.detail.value)
    this.setData({
      search: event.detail.value
    })
  },

  // es进行查询
  Search() {
    console.log(this.data.search)
    const that = this; // 保存this的引用，因为wx.request的回调函数中this指向会变化
    const token = wx.getStorageSync('token');
    console.log("存储 token:", token);

    if (!token) {
      console.warn('没有找到 token');
      return;
    }

    wx.request({
      url: `${API_BASE_URL}/product/search`, // 使用实际接口URL
      method: 'POST',
      data: {
        keyWord: that.data.search,
        current: that.data.current,
        size: that.data.size
      },
      header: {
        'content-type': 'application/json', // 默认值是 application/json
        'satoken': token // 添加 token 到请求头
      },
      success(res) {
        const records = res.data.data;
        console.log("goodList", records)
        that.setData({
          goodsList: records.productList,
          highlightList: records.highlightList,
          search: ""
        })
        console.log("goodList",that.data.goodsList),
        console.log("highlightList",that.data.highlightList)
      },
      fail(err) {
        console.error('网络请求失败', err);
      }
    });

  },

  totest(){

  },

  // 商品详情跳转
  toGoodDetail(event) {
    console.log(event)
    let id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goodDetail/goodDetail?id=' + id,
    })
  }
})